package com.chengqj.core.datasource;

import com.chengqj.core.datasource.property.OrderDatasourceProperties;
import com.mysql.cj.jdbc.MysqlXADataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Copyright © 2020年 com.qxkj. All rights reserved.
 *
 * @author chengqj
 * 订单配置类
 * version 1.0.0 (接口必须带上)
 * @date 2020/11/6 15:24
 */

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = {"com.chengqj.order.repository"},
        entityManagerFactoryRef = "orderEntityManagerFactory",
        transactionManagerRef = "transactionManager"
)
public class OrderDataSourceConfig {
    @Autowired
    private JpaVendorAdapter jpaVendorAdapter;

    @Bean(name = "orderDataSource")
    public DataSource dataSource(OrderDatasourceProperties orderDatasourceProperties) throws SQLException {
        MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
        mysqlXaDataSource.setUrl(orderDatasourceProperties.getUrl());
        mysqlXaDataSource.setPassword(orderDatasourceProperties.getPassword());
        mysqlXaDataSource.setUser(orderDatasourceProperties.getUsername());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        xaDataSource.setXaDataSource(mysqlXaDataSource);
        xaDataSource.setUniqueResourceName("orderDB");
        xaDataSource.setMaxPoolSize(20);
        return xaDataSource;

    }

    @Bean(name = "orderEntityManagerFactory")
    @DependsOn({"atomikosJtaPlatform"}) //需要先注册atomikosJtaPlatform
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("orderDataSource") DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setJtaDataSource(dataSource);
        entityManagerFactory.setPackagesToScan("com.chengqj.order.domain");
        entityManagerFactory.setPersistenceUnitName("orderPersistenceUnit");
        entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);

        Properties properties = new Properties();
        //jta设置
        properties.put("hibernate.current_session_context_class", "jta");
        properties.put("hibernate.transaction.factory_class", "org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory");
        //这里指定我们自己创建的AtomikosJtaPlatform
        properties.put("hibernate.transaction.jta.platform", "com.chengqj.core.transaction.jta.platform.AtomikosJtaPlatform");
        entityManagerFactory.setJpaProperties(properties);

        return entityManagerFactory;
    }
}
